package com.example.administrator.easy.adapter;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.easy.R;
import com.example.administrator.easy.model.GuangFxResult;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2016/11/15.
 */

public class GuangFxLvAdapter extends BaseAdapter {//
    private Context mContext;
    private ArrayList<GuangFxResult> mList;
    private ImageOptions options;
    private MediaPlayer player;
    private int mCurPosition = -1;
    private String url;
    public GuangFxLvAdapter(Context context,ArrayList<GuangFxResult> list){
        mContext = context;
        mList = list;
        options = new ImageOptions.Builder().setUseMemCache(true).build();
        player = new MediaPlayer();//初始化播放器
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        Log.e("Maindssss",mList.get(position).getArticlePictures().size()+"");
        if(mList.get(position).getArticlePictures().size()==0){
            Log.e("Maindsssssssss","null");
            return 2;//视频
        }else if(mList.get(position).getArticlePictures().size()==3){
            return 1;//三张图片的
        }
        return 0;//一张图片的
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
      switch (getItemViewType(position)){
          case 0://布局一
              ViewHolder1 vh1;
              if(convertView==null){
                  convertView = View.inflate(mContext,R.layout.fragment_guang_fx_item_one,null);
                  vh1 = new ViewHolder1(convertView);
                  convertView.setTag(vh1);
              }else{
                  vh1 = (ViewHolder1) convertView.getTag();
              }
              x.image().bind(vh1.ivTou,mList.get(position).getUserAvatar(),options);
              x.image().bind(vh1.ivRight,mList.get(position).getArticlePictures().get(0).getPicPath());
              if(TextUtils.isEmpty(mList.get(position).getUserNickName())){
                  vh1.tvName.setText("匿名");
              }else{
                  vh1.tvName.setText(mList.get(position).getUserNickName());
              }

              String time = mList.get(position).getPublishDate();
              String jie = time.substring(6,19);
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM--dd HH:mm");
              Long in = Long.parseLong(jie);
              Date date= new Date(in);
              Log.e("Maindate",sdf.format(date)+"");
              vh1.tvTime.setText(sdf.format(date));
              vh1.tvTitle.setText(mList.get(position).getTitle());
              vh1.tvLaizi.setText("来自 "+mList.get(position).getStarName());
              break;
          case 1://布局二
                ViewHolder2 vh2;
              if(convertView==null){
                  convertView = View.inflate(mContext,R.layout.fragment_fx_item_two,null);
                  vh2 = new ViewHolder2(convertView);
                  convertView.setTag(vh2);
              }else{
                  vh2 = (ViewHolder2) convertView.getTag();
              }
              x.image().bind(vh2.ivTou,mList.get(position).getUserAvatar());
              x.image().bind(vh2.iv1,mList.get(position).getArticlePictures().get(0).getPicPath());
              x.image().bind(vh2.iv2,mList.get(position).getArticlePictures().get(1).getPicPath());
              x.image().bind(vh2.iv3,mList.get(position).getArticlePictures().get(2).getPicPath());
              if(TextUtils.isEmpty(mList.get(position).getUserNickName())){
                  vh2.tvName.setText("匿名");
              }else{
                  vh2.tvName.setText(mList.get(position).getUserNickName());
              }
             // Log.e("Mainname",mList.get(position).getUserNIckName()+"sdds");

              String time1 = mList.get(position).getPublishDate();
              String jie1 = time1.substring(6,19);
              SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM--dd HH:mm");
              Long in1 = Long.parseLong(jie1);
              Date date1= new Date(in1);
              vh2.tvTime.setText(sdf1.format(date1));
              vh2.tvLaizi.setText("来自 "+mList.get(position).getStarName());
              break;
          case 2://布局三
              ViewHolder3 vh3;
              if(convertView==null){
                  convertView = View.inflate(mContext,R.layout.fragment_gaung_item_three,null);
                  vh3 = new ViewHolder3(convertView);
                  convertView.setTag(vh3);
              }else{
                  vh3 = (ViewHolder3) convertView.getTag();
              }
              x.image().bind(vh3.ivTou,mList.get(position).getUserAvatar());
              if(TextUtils.isEmpty(mList.get(position).getUserNickName())){
                  vh3.tvName.setText("匿名");
              }else{
                  vh3.tvName.setText(mList.get(position).getUserNickName());
              }

              String time2 = mList.get(position).getPublishDate();
              String jie2 = time2.substring(6,19);
              SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM--dd HH:mm");
              Long in2 = Long.parseLong(jie2);
              Date date2= new Date(in2);
              vh3.tvTime.setText(sdf2.format(date2));
              vh3.tvLaizi.setText("来自 "+mList.get(position).getStarName());

              //正在播放的Item滑出屏幕
              Integer tag = (Integer) vh3.tvLaizi.getTag();
              //tag不等于空的（item正在被复用）
              if(tag!=null&&tag==mCurPosition&&mCurPosition!=position){
                if(player.isPlaying()){
                    player.pause();
                }
                  mCurPosition=-1;
                  player.reset();
              }

              vh3.tvLaizi.setTag(position);
              //播放视频
              //1,设置监听
              player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                  @Override
                  public void onPrepared(MediaPlayer mp) {
                      mp.start();
                  }
              });
              //2,获取SurfaceView的SurfaceHolder
              SurfaceHolder surfaceHolder = vh3.sur.getHolder();

              url = mList.get(position).getIco();
              Log.e("Mainurl",url);
              //3,关联SurfaceView和MediaPalyer(之前，必须保证SurfaceView初始化好了)
              surfaceHolder.addCallback(new SurfaceHolder.Callback() {

                  //surfaceView创建好了
                  @Override
                  public void surfaceCreated(SurfaceHolder holder) {
                      //3,关联surfaceView和MediaPlayer
                      player.setDisplay(holder);
                      //4,设置播放路径
                      try {
                          player.setDataSource(url);
                          player.prepareAsync();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }

                  @Override
                  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

                  }

                  @Override
                  public void surfaceDestroyed(SurfaceHolder holder) {

                  }
              });
              break;
      }
        return convertView;
    }

    class ViewHolder1{//布局一：一张图片的
        ImageView ivTou,ivRight;
        TextView tvName,tvTime,tvTitle,tvLaizi;
        public ViewHolder1(View view){
            ivTou = (ImageView) view.findViewById(R.id.ivtouxiang);
            ivRight = (ImageView) view.findViewById(R.id.ivrigth);
            tvName = (TextView) view.findViewById(R.id.tvxiaobian);
            tvTime = (TextView) view.findViewById(R.id.tvtime);
            tvTitle = (TextView) view.findViewById(R.id.tvcontent);
            tvLaizi = (TextView) view.findViewById(R.id.tvlaizi);
        }
    }

    class ViewHolder2{
        ImageView ivTou,iv1,iv2,iv3;
        TextView tvName,tvTime,tvLaizi;
        public ViewHolder2(View view){
            ivTou = (ImageView) view.findViewById(R.id.twoivtouxiang);
            iv1 = (ImageView) view.findViewById(R.id.tiv1);
            iv2 = (ImageView) view.findViewById(R.id.tiv2);
            iv3 = (ImageView) view.findViewById(R.id.tiv3);
            tvName = (TextView) view.findViewById(R.id.twotvxiaobaian);
            tvTime = (TextView) view.findViewById(R.id.twotvtime);
            tvLaizi = (TextView) view.findViewById(R.id.twotvlaizi);
        }
    }

    class ViewHolder3{
        ImageView ivTou;
        TextView tvName,tvTime,tvLaizi;
        SurfaceView sur;
        public ViewHolder3(View view){
            ivTou = (ImageView) view.findViewById(R.id.tivtouxiang);
            tvName = (TextView) view.findViewById(R.id.ttvxiaobaian);
            tvTime= (TextView) view.findViewById(R.id.ttvtime);
            tvLaizi = (TextView) view.findViewById(R.id.ttvlaizi);
            sur = (SurfaceView) view.findViewById(R.id.fx_surface);
        }
    }
}
